﻿// ***************************************************************
//  version:  0.4.0   date: 11/15/2007-2008
//  -------------------------------------------------------------
//  
//  -------------------------------------------------------------
//  Copyright (C) Midapex 2003-2007-2008 All Rights Reserved
// ***************************************************************
// 
// ***************************************************************
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Midapex.SmartOrm
{
    class PropertyRelationTable:RelationTable
    {
        PropertyInfo prop;

        public PropertyRelationTable(EntityColumnAttribute mcatrr, Table parent,
            Column parentPrimaryCol, PropertyInfo prop)
            : base( parent, parentPrimaryCol)
        {
            this.prop = prop;
            //设置关联表的名称
            if (mcatrr.RelationTableName == null || mcatrr.RelationTableName == string.Empty)
            {
                //默认使用(父表名称_成员名称）作为关联表的名称
                name = parent.Name + "_" + prop.Name;
            }
            else
            {
                name = mcatrr.RelationTableName;
            }

            name = name.ToUpper();
        }


        public override object Read(object target)
        {
            if (prop.GetGetMethod() == null)
                return null;

            return prop.GetGetMethod().Invoke(target, null);
        }

        public override void Write(object target, object value)
        {
            if (prop.GetSetMethod() == null)
                return;
            prop.GetSetMethod().Invoke(target, new object[] { value });
        }
        public override Type ClassMemberType
        {
            get
            {
                return prop.PropertyType;
            }
        }
    }
}
